﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace vuonthieunhi.Models
{
    public class PaymentDao : BaseDao
    {
        /// <summary>
        /// Insert a question without answer
        /// </summary>
        /// <param name="question"></param>
        /// <returns>int</returns>
        public int Insert(Payment payment)
        {
            int result = 0;
            try
            {
                if (payment != null)
                {
                    dbContext.Payments.InsertOnSubmit(payment);
                    //Submit changes to database
                    dbContext.SubmitChanges();
                    result = payment.ID;
                }
            }
            catch
            {
                result = 0;
            }
            return result;
        }

        public void Insert(Payment objUI, List<Order> orderList)
        {
            if (objUI != null)
            {
                //insert and get it's id
                int paymentId = Insert(objUI);
                if (paymentId != 0)
                {
                    List<PaymentDetail> paymentDetaiList = new List<PaymentDetail>();
                    foreach (Order item in orderList)
                    {
                        PaymentDetail detail = new PaymentDetail();
                        detail.ProductId = item.Id;
                        detail.PaymentID = paymentId;
                        detail.Price = item.Price;
                        detail.Quantity = item.Quantity;
                        detail.Subtotal = item.SubTotal;

                        paymentDetaiList.Add(detail);
                    }
                    dbContext.PaymentDetails.InsertAllOnSubmit(paymentDetaiList);
                    dbContext.SubmitChanges();
                }
            }
        }
    }
}